<?php

namespace App\Jobs;

use App\Model\NetworkInfo;
use Illuminate\Bus\Queueable;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SaveNetworkUser implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public $request;

    /**
     * Create a new job instance.
     *
     * @param array
     */
    public function __construct($request)
    {
        $this->request = $request;
    }

    /**
     * Execute the job.
     *
     * @param Request $request
     */
    public function handle()
    {
        $networkInfo = NetworkInfo::firstOrNew(['number' => $this->request['mainForm:userName']]);

        $networkInfo->open_id = $this->request['openId'];

        $networkInfo->address = $this->getAddressByRequest();

        $networkInfo->number = $this->request['mainForm:userName'];

        $networkInfo->password = isset($this->request['remember']) && $this->request['remember'] === 'on' ? $this->request['mainForm:userPasswd'] : '';

        $networkInfo->save();
    }

    /**
     * 表单中获取地址
     *
     * @return string
     */
    private function getAddressByRequest()
    {
        return $this->request['COLUMN_02'] . '栋' . $this->request['COLUMN_04'] . $this->request['COLUMN_05'];
    }
}
